package cn.m3a1.task;

import cn.m3a1.dao.UserDao;
import cn.m3a1.model.Course;
import cn.m3a1.model.Syslog;
import cn.m3a1.model.User;
import com.iceking.dynamicweb.FrmkView;
import com.iceking.dynamicweb.GlobalUtils;
import com.iceking.dynamicweb.Pagination;
import com.iceking.dynamicweb.TaskProcessor;

import java.util.List;

/**
 * Created by wyj on 2019-06-07.
 */
public class AdminSys extends TaskProcessor {
    private User loginUser;
    public Boolean panduan(){
        loginUser = (User) getSession().getAttribute("loginUser");
        if (loginUser==null){
            return false;
        }

        int type=loginUser.getType();

        if (type!=99){
            return false;
        }
        return  true;
    }
    /**
     * 转发跳转到管理员管理页面
     * @throws Exception
     */
    public void home() throws  Exception{
        if (!panduan()){
            return;
        }

        String who = getParam("name");

        List list;
        FrmkView frmkView;

        int type=loginUser.getType();
        //验证身份
        if (type==99){
            //判断是否是超级管理员
            frmkView=new FrmkView("AdminSys.html");
            //根据用户名和密码去进行数据库访问
            Pagination<Syslog> pg = new Pagination<>();

                if (who!=null){
                    pg.appendWhere("where who like ?","%"+who+"%");
                }

                 getDBConn().select(Syslog.class, pg);
                    list = pg.getRows();



            frmkView.addData("members",list);
            //判断是否是班主任
        }else{
            //非法用户
            frmkView=new FrmkView("error.html");
        }

        frmkView.addData("loginUser",loginUser);
        toFreemarker(frmkView);

    }
    /**
     * 根据id删除数据
     * @throws Exception
     */
    public void del() throws Exception {
        if (!panduan()){
            return;
        }
        int id = getParamInt("id");
        System.out.println("del"+id);
        Pagination<Syslog> pg = new Pagination<>();
        pg.appendWhere("where id=?", id);
        getDBConn().select(Syslog.class, pg);
        Syslog syslog = pg.getFirstResult();
        getDBConn().delete(syslog);

    }
}
